package com.yixun.qfbao.hander;

import com.alibaba.fastjson.JSONObject;
import com.yixun.qfbao.api.service.V10BindService;
import com.yixun.qfbao.apidto.PremisesIntentionReqDto;
import com.yixun.qfbao.apidto.PremisesIntentionResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.dto.ApiLogDto;
import com.yixun.qfbao.util.email.MailConfig;
import com.yixun.qfbao.util.email.MailUtil;
import com.yixun.qfbao.vo.PremisesIntentionReqVo;
import com.yixun.qfbao.xf.model.enums.ConfirmStatusEnum;
import com.yixun.qfbao.xf.model.enums.CustomerStatusEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: channel-parent
 *
 * @description:
 *
 * @author: Seven.wang
 *
 * @create: 2019-10-31 11:29
 **/
@Service("queryIntentionStatusHander")
public class QueryIntentionStatusHander extends CommonAbstractHander {

    private Logger logger = LoggerFactory.getLogger(QueryIntentionStatusHander.class);

    @Autowired
    V10BindService v10BindService;

    @Override
    public BaseResponseDTO<PremisesIntentionResDto> doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<PremisesIntentionResDto> res = new BaseResponseDTO();
        PremisesIntentionResDto resDto = new PremisesIntentionResDto();
        PremisesIntentionReqVo reqVo = (PremisesIntentionReqVo)reqDto;
        PremisesIntentionReqDto premisesIntentionReqDto = reqVo.getParams();
        resDto.setResult(ResultEnum.FAIL.getNo());
        try{
            resDto = v10BindService.queryIntentionStatus(premisesIntentionReqDto.getIntentionUuid(), "1");
            if(ResultEnum.FAIL.getNo().equals(resDto.getResult())){
                res.setResult(resDto.getResult());
                res.setErrorMsg(resDto.getResultMsg());
                return res;
            }
            StringBuffer sb = new StringBuffer("自动补查报备状态：");
            sb.append("公司UUID:").append(premisesIntentionReqDto.getCompanyUuid());
            sb.append("报备UUID:").append(premisesIntentionReqDto.getIntentionUuid());
            sb.append("报备审核状态为:").append(resDto.getAuditStatus());
            sb.append("报备交易状态为:").append(resDto.getTradeStatus());
            MailUtil.sendProcess(buildMailConfig(), "bin.wang@qiaofang.cn", null, null, "[自动补推]同步V10报备状态失败", sb.toString(), null);
            res.setResult(resDto.getResult());
            res.setResData(resDto);
        }catch (Exception e){
            logger.error("queryIntentionStatusHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setResult(e.getMessage());
        }

        return res;
    }

    private MailConfig buildMailConfig(){
        MailConfig config = new MailConfig();

        config.setSmtp("smtp.exmail.qq.com");
        config.setPort("25");
        config.setEmail("songjianhua@yixuninfo.com");
        config.setEmailName("JHSong");
        config.setUserName("songjianhua@yixuninfo.com");
        config.setPassword("12345Abcde");
        return config;
    }
}
